<?php
/**
 * @name /www/EditUser.php
 * @author
 * @version 2011.03.30
 *
 * This is the page for Editing User Information by the Administrator.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');

// Libraries
ReqLib('/lib/private/User.php');


function DefineHeader() {
?><script type="text/javascript">
  function onModifyRoles(select, value, text) {
    $roles = $('#RolesSelect Option');
    var text = '';
    $.each($roles, function(i, role) {
      text += (i ? '|' : '') + role.value;
    });
    $('#RolesHidden').val(text);
  }

  $(function() {
    $('#RolesSelect').selectList({
      onAdd: onModifyRoles,
      onRemove: onModifyRoles
    });
    $('form table').dataTable({
      bJQueryUI: true,
      bSort: false,
      bFilter: false,
      bPaginate: false,
      bInfo: false,
      fnInitComplete: function() {
        $('form table td').css('padding', '4px 6px');
      }
    });
    $('input[type=button], input[type=submit]').button();
  });
</script>
<?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  // Page content goes here.
  ?>
<p>
  <a href="<?=_webroot('/') ?>">Home</a>.
</p>
<?
	if($_SESSION['userId'] >0){
?>
		<p>
		  <a href="<?=_webroot('/account.php') ?>">My Account</a>.
		</p>
		<p>
		  <a href="<?=_webroot('/logout.php') ?>">Logout</a>.
		</p>
    <form action="userRequestHandle.php" method="POST">
<center style="width: 310px; margin-left: auto; margin-right: auto;">
<table style="border: 1px solid black;" cellspacing="0" class="Display" width="100%">
  <thead>
    <tr style="display: none;">
      <th>a
      </th>
      <th>b
      </th>
    </tr>
  </thead>
	<tbody>
<?
	$u = new User();
	$userId = $_REQUEST['uId'];

	//print($userId);/

	$u->Load($userId);

	//print_r($u);
	//die();

	print('<tr><td>User ID</td><td><input type="text" name="UserId" value="'.$u->UserId.'" /></td></tr>');
	print('<tr><td>User Name</td><td><input type=text name=UserName value="'.$u->UserName.'" /></td></tr>');
	print('<tr><td>First Name</td><td><input type=text name=FirstName value="'.$u->FirstName.'" /></td></tr>');
	print('<tr><td>Last Name</td><td><input type=text name=LastName value="'.$u->LastName.'" /></td></tr>');
	print('<tr><td>Phone Number</td><td><input type=text name=PhoneNumber value="'.$u->PhoneNumber.'" /></td></tr>');
	print('<tr><td>Email</td><td><input type=text name=Email value="'.$u->Email.'" /></td></tr>');
	print('<tr><td>Street 1</td><td><input type=text name=Street1 value="'.$u->Street1.'" /></td></tr>');
	print('<tr><td>Street 2</td><td><input type=text name=Street2 value="'.$u->Street2.'" /></td></tr>');
	print('<tr><td>Street 3</td><td><input type=text name=Street3 value="'.$u->Street3.'" /></td></tr>');
	print('<tr><td>City</td><td><input type=text name=City value="'.$u->City.'" /></td></tr>');
	print('<tr><td>State</td><td><input type=text name=State value="'.$u->State.'" /></td></tr>');
	print('<tr><td>Country</td><td><input type=text name=Country value="'.$u->Country.'" /></td></tr>');
	print('<tr><td>Postal Code</td><td><input type=text name=PostalCode value="'.$u->PostalCode.'" /></td></tr>');
	print('<tr><td>News Letter Subscription</td><td><input type=checkbox name=NewsletterSubscription value="yes"');

	if($u->NewsletterSubscription == 0)
		print(' /></td></tr>');
	else
		print(' checked /></td></tr>');
	print('<tr><td>Notes</td><td><input type=text name=Notes value="'.$u->Notes.'" /></td></tr>');
  // Get all roles as an array
  $rolesOptions = User::GetRoles();
  array_push($rolesOptions, 'Select Role...');
  // For each role in the array, wrap it with HTML option markup
  for ($i = 0; $i < count($rolesOptions); $i++) {
    $o = $rolesOptions[$i];
    $o = '<option value="' . htmlspecialchars($o) . '"' . ($u->HasRole($o) ? ' selected="selected"' : '') . '>' . htmlspecialchars($o) . '</option>';
    $rolesOptions[$i] = $o;
  }
  // Turn the array into a string
  $rolesOptions = implode('', $rolesOptions);
  // Spit out the roles as an HTML select element
	print('<tr><td>Roles</td><td><select id="RolesSelect" multiple="multiple" title="Select Role...">' . $rolesOptions . '</select></td></tr>');
	print('</tbody></table>');
	print('</center>');
?><center style="margin-top: 10px;">
<input type="submit" name="Action" value="Update User Info" />
<input type="submit" name="Action" value="Delete User" />
<input type="button" value="Back" onClick="history.go(-1)" />
</center>
<?
	print('<input type="hidden" name="Roles" id="RolesHidden" value="' . implode('|', $u->_Roles) . '" />');

  	print('</form>');
	}
   else{
?>
		<p>
		  <a href="<?=_webroot('/login_register.php') ?>">Login/Register</a>.
		</p>
<?
   }
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');